var taskdetail = {
    conf: {
        image_arr: [],
        opercount: 0
    },
    init: function (d) {
        $.extend(this.conf, d);
        this.bindBtn();
    },
    bindBtn: function () {
        this.btnUpload();
        this.btnSubmit();
        this.btnGive();
    },
    btnUpload: function () {
        var _self = this;
        $(".uploadbtn").click(function () {
            wx.chooseImage({
                // 默认9
                count: _self.conf.image_count,
                // 可以指定是原图还是压缩图，默认二者都有
                sizeType: ['original', 'compressed'],
                // 可以指定来源是相册还是相机，默认二者都有
                sourceType: ['album', 'camera'],
                success: function (res) {
                    // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                    if (res.localIds.length + _self.conf.opercount !== _self.conf.image_count) {
                        layer.alert('对不起!图片上传数量不符，需要' + _self.conf.image_count + '张',{icon:5});
                    } else {
                        for (var i = 0; i < res.localIds.length; i++) {
                            if (typeof (res.localIds[i]) !== "undefined") {
                                var html = '<div class="group img"><img height="65px" width="65px" src="' + res.localIds[i].toString() + '"/></div>';
                                $(".uploadbtn").before(html);
                                $(".img img").unbind("click").bind("click", function (e) {
                                    _self.conf.opercount = _self.conf.opercount - 1;
                                    $(this).parent().remove();
                                });
                                _self.conf.opercount = i + 1;
                                wx.uploadImage({
                                    localId: res.localIds[i].toString(),
                                    // 默认为1，显示进度提示
                                    isShowProgressTips: 1,
                                    success: function (e) {
                                        // 返回图片的服务器端ID
                                        _self.conf.image_arr.push(e.serverId);
                                    }
                                });
                            }
                        }
                    }
                },
                cancel: function () {

                }
            });
        });
    },
    btnSubmit: function () {
        var _self = this;
        $(".btn-taskdetail").click(function () {
            if (_self.conf.opercount === parseInt(_self.conf.image_count)) {
                $.ajax({
                    type: "post",
                    url: "/task/taskdetail.html",
                    data: {id: _self.conf.id, images: _self.conf.image_arr.join(","), taskaccount: ($("#taskaccount") !== "undefined" ? $("#taskaccount").val() : null)},
                    dataType: "json",
                    success: function (data) {
                        if (data.status === true) {
                            window.location.href = data.url;
                        } else {
                            alert(data.msg);
                        }
                    }
                });
            } else {
                layer.alert('对不起!图片上传数量不符，需要' + _self.conf.image_count + '张',{icon: 5});
            }
        });
    },
    btnGive: function () {
        var _self = this;
        $(".give-up").click(function () {
            if (confirm("确定要隐藏任务吗？")) {
                $.post("/task/hidetask.html", {id: _self.conf.id, action: "hide"}, function (data) {
                    if (data.status) {
                        window.location.href = data.url;
                    } else {
                        layer.alert(data.msg, {icon: 5});
                    }
                }, 'json');
            }
        });
    }
};